package com.smart.nine.spel.expr.object;

import com.smart.nine.spel.expr.User;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.*;

import static javafx.scene.input.KeyCode.O;

/**
 * 赋值
 *
 * @author junrui.wang
 * @create 2019/5/15
 */
public class ObjectExprSample {
    public static void main(String[] args) {
        //1.赋值
        User user = new User();
        user.setName("Tom");

        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext(user);
        //通过setValue赋值
        parser.parseExpression("Name").setValue(user, "John");
        System.out.println(user.getName());

        //通过表达式赋值
        parser.parseExpression("Name='Peter'").getValue(context);
        System.out.println(user.getName());


        //2.类型
        //功能类似于ClassLoader.loadClass()方法
        Class className = parser.parseExpression("T(java.lang.String)").getValue(Class.class);
        System.out.println(className == java.lang.String.class);

        //如果加载的类在java.lang包下可以不加包名，其他的必须加上完整的包名
        Class className1 = parser.parseExpression("T(String)").getValue(Class.class);
        System.out.println(className1 == java.lang.String.class);

        Class className2 = parser.parseExpression("T(com.smart.nine.spel.expr.User)").getValue(Class.class);
        System.out.println(className2 == com.smart.nine.spel.expr.User.class);

        //T可以直接调用类静态方法'T(全限定类名).静态方法'
        Object randomValue = parser.parseExpression("T(java.lang.Math).random()").getValue();
        System.out.println(randomValue);

        //3.构造器
        //除了基本类型和字符串，其他类都需要使用全限定类名
        String string = parser.parseExpression("new String('Tom')").getValue(String.class);
        System.out.println(string);

        User user1 = parser.parseExpression("new com.smart.nine.spel.expr.User('Tom', 100)").getValue(User.class);
        System.out.println(user1);

        //4.变量
        User user2 = new User("Tom", 1000);
        StandardEvaluationContext context1 = new StandardEvaluationContext(user2);
        //设置变量
        context1.setVariable("newName", "John");

        //取变量值，赋值
        parser.parseExpression("name=#newName").getValue(context1);
        System.out.println(user2.getName());

        //this变量值使用
        List<Integer> credits = new ArrayList<>();
        credits.addAll(Arrays.asList(100,200,300,50,40));

        context1.setVariable("credits", credits);
        List<Integer> credits100 = (List<Integer>) parser.parseExpression("#credits.?[#this>100]").getValue(context1);
        System.out.println(credits100);

        //5.集合过滤
        StandardEvaluationContext context2 = new StandardEvaluationContext();
        Map creditsMap = new HashMap<>();
        creditsMap.put("Tom", 99);
        creditsMap.put("Tom1", 100);
        creditsMap.put("Tom2", 101);
        creditsMap.put("Tom3", 102);

        context2.setVariable("credits", creditsMap);
        //过滤语法?[条件]，返回第一个^[条件]，返回最后一个$[条件]
        Map creditsMap100 = (Map) parser.parseExpression("#credits.?[value>100]").getValue(context2);
        System.out.println(creditsMap100);

        Map creditsMap100First = (Map) parser.parseExpression("#credits.^[value>100]").getValue(context2);
        System.out.println(creditsMap100First);

        Map creditsMap100Last = (Map) parser.parseExpression("#credits.$[value>100]").getValue(context2);
        System.out.println(creditsMap100Last);

    }
}
